cloudmlmagic Example

Example notebook to use cloudmlmagic. Before you begin, install cloudmlmagic.

$ pip install cloudmlmagic

In [ ]:
#Load cloudmlmagic extention
%load_ext cloudmlmagic

In [ ]:
# Initialize Cloud ML Engine client library
# Make sure you call this magic before adding code or run

%ml_init -projectId PROJECTID -bucket BUCKET -scaleTier BASIC

In [ ]:
# This code block won't be uploaded.
# Use this as preprocessing or diagnostic etc..

print('This is..')
print('only for...')
print('local execution!')
meaning_of_life = 42  # <- this variable is visible only on local

In [ ]:
%%ml_code
# ML Code to be uploaded.
# Note that any variables/packages used outside of ml_code block
# are not visible when running on cloud ml.

import tensorflow as tf
from sklearn import cross_validation
import logging

def run_training():
    # Load iris dataset
    iris = tf.contrib.learn.datasets.base.load_iris()
    train_x, test_x, train_y, test_y = cross_validation.train_test_split(
        iris.data, iris.target, test_size=0.2
    )

    
    feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
    classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                                hidden_units=[10, 20, 10],
                                                n_classes=3,
                                                model_dir="./model")

    classifier.fit(x=train_x,
                   y=train_y,
                   steps=2000,
                   batch_size=50)

In [ ]:
%%ml_code
# You can devide code blocks like this.

a = 42  # <- this variable is visible when executing job on cloud ml

In [ ]:
%%ml_run cloud
# Run a training job.
# Code in this block is also uploaded.
# To see your cloud ml engine job, go to https://console.cloud.google.com/mlengine/jobs

run_training()